﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SksApp
{
    public partial class ProductGroupView : BaseView
    {
        public ProductGroupView()
        {
            InitializeComponent();
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            this.Dispose();
            Event.Invoke(null, null);
        }

        private void ProductGroupView_Load(object sender, EventArgs e)
        {
            cmbGrup.SelectedIndex = 0;
            using (SksEntity ent = new SksEntity())
            {
                cmbUrun.Items.Add(new Stok() { ID = -1, Adi = "" });
                cmbUrun.Items.AddRange(ent.Stoklar.ToArray());
            }
        }

        private void txtBarkod_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                UrunBul(txtBarkod.Text);
            }
        }

        private void UrunYukle(int groupNo)
        {
            listBox1.Items.Clear();
            txtAdi.Text = "";
            txtBarkod.Text = "";
            using (SksEntity ent = new SksEntity())
            {
                int grpno = Cevir.Parse<int>(cmbGrup.Text);
                var grp = ent.UrunGruplari.Where(x => x.No == grpno).FirstOrDefault();
                if (grp != null)
                {
                    txtAdi.Text = grp.Adi;
                    var tuslar = ent.Tuslar.Where(x => x.GroupNo == grpno).ToList();
                    foreach (var item in tuslar)
                    {
                        listBox1.Items.Add(item);
                    }
                }
            }
        }

        private void UrunBul(string barkod)
        {
            using (SksEntity ent = new SksEntity())
            {
                var brkd = ent.Barkodlar.Where(x => x.Barkodu == txtBarkod.Text).FirstOrDefault();
                if (brkd != null)
                {
                    for (int i = 0; i < cmbUrun.Items.Count; i++)
                    {
                        if (((Stok)cmbUrun.Items[i]).ID == brkd.StokID)
                        {
                            cmbUrun.SelectedIndex = i;
                            break;
                        }
                    }
                }
            }
        }

        private void btnEkle_Click(object sender, EventArgs e)
        {
            if (cmbUrun.SelectedIndex > 0)
            {
                Tus tus = new Tus();
                tus.Barkod = txtBarkod.Text;
                tus.GroupNo = Cevir.Parse<int>(cmbGrup.Text);
                tus.Image = null;
                tus.Adi = cmbUrun.Text;
                listBox1.Items.Add(tus);
            }
        }

        private void btnCikar_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex > -1)
            {
                listBox1.Items.RemoveAt(listBox1.SelectedIndex);
            }
        }

        private void btnKaydet_Click(object sender, EventArgs e)
        {
            if (cmbUrun.SelectedIndex > 0)
            {
                using (SksEntity ent = new SksEntity())
                {
                    int grpno = Cevir.Parse<int>(cmbGrup.Text);
                    var grp = ent.UrunGruplari.Where(x => x.No == grpno).FirstOrDefault();
                    if (grp != null)
                    {
                        grp.Adi = txtAdi.Text;
                        var tuslar = ent.Tuslar.Where(x => x.GroupNo == grpno).ToList();
                        foreach (var item in tuslar)
                        {
                            ent.Tuslar.Remove(item);
                        }
                    }
                    else
                    {
                        grp = new UrunGrup();
                        grp.Adi = txtAdi.Text;
                        grp.No = grpno;
                        ent.UrunGruplari.Add(grp);
                    }

                    for (int i = 0; i < listBox1.Items.Count; i++)
                    {
                        Tus tus = (Tus)listBox1.Items[i];
                        ent.Tuslar.Add(tus);
                    }

                    ent.SaveChanges();
                }
            }
        }

        private void btnSil_Click(object sender, EventArgs e)
        {
            using (SksEntity ent = new SksEntity())
            {
                int grpno = Cevir.Parse<int>(cmbGrup.Text);
                var grp = ent.UrunGruplari.Where(x => x.No == grpno).FirstOrDefault();
                if (grp != null)
                {
                    grp.Adi = txtAdi.Text;
                    var tuslar = ent.Tuslar.Where(x => x.GroupNo == grpno).ToList();
                    foreach (var item in tuslar)
                    {
                        ent.Tuslar.Remove(item);
                    }

                    ent.UrunGruplari.Remove(grp);
                }
                ent.SaveChanges();
            }
        }

        private void cmbGrup_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbGrup.SelectedIndex > -1)
            {
                UrunYukle(Cevir.Parse<int>(cmbGrup.Text));
            }
        }
    }
}
